package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.domain.params.SysProductParams;
import com.ruoyi.system.service.ISysProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 产品Controller
 *
 * @author liuxing
 * @date 2021-02-03
 */
@RestController
@RequestMapping("/system/sys_product")
public class SysProductController extends BasePlusController {
    @Autowired
    private ISysProductService sysProductService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysProductParams sysProductParams) {
        startPage();
        List<SysProduct> list = sysProductService.selectSysProductList(sysProductParams);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysProductParams sysProductParams) throws IOException {
        List<SysProduct> list = sysProductService.selectSysProductList(sysProductParams);
        ExcelUtil<SysProduct> util = new ExcelUtil<SysProduct>(SysProduct.class);
        util.exportExcel(response, list, "sysProduct");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysProductService.selectSysProductById(id));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody SysProduct sysProduct) {
        return R.ok(sysProductService.insertSysProduct(sysProduct));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody SysProduct sysProduct) {
        return R.ok(sysProductService.updateSysProduct(sysProduct));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return R.ok(sysProductService.deleteSysProductByIds(ids));
    }

    /**
     * 启用产品
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:enable')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping("/enable/{id}")
    public R enable(@PathVariable Long id) {
        return R.ok(sysProductService.enableSysProduct(id));
    }

    /**
     * 停用产品
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_product:stop')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{id}")
    public R stop(@PathVariable Long id) {
        return R.ok(sysProductService.stopSysProduct(id));
    }
}
